<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Crew extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'crew';         
        $this->load->model('Mcrew');
    }
    
    public function index()
    {
        $this->data['id_active'] = $this->getParamUri(2,2);
        $this->load->library('pagination');
        $config['per_page'] = 30;
        $this->data['page'] = $this->data['pageg'] = $this->data['pageCurrent'] = $this->getParamUri(2,2);
        $data = $this->Mcrew->getCrewDB($config['per_page'],$this->getParamUri(2,2));
        //var_dump($data);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,2);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
	    $this->data['action'] = 'index';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addcrew()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCrew', '- Tên thuyền viên', 'required');
        $this->form_validation->set_rules('rank', '- Rank', 'required');
        $this->form_validation->set_rules('sex', '- Giới tính', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'vinalines-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcrew->addCrewDb($img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thuyền viên thành công");window.location.href="'.$this->data['baseurl'].'crew.html";</script>');
            }
        }
        $this->data['sex_option'] = $this->Mcrew->sexOption();
        $this->data['action'] = 'addcrew';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editcrew()
    {
        $this->data['id'] = $this->getParamInt('id');
        //var_dump($data['id']);die('truongvx');
        $pageCurrent = $this->data['pageCurrent'] = $this->getParamInt('page') ? $this->getParamInt('page') : 0;
        $this->data['data'] = $this->Mcrew->getCrewEdit($this->data['id']);
       
        $this->data['sex_option'] = $this->Mcrew->sexOption($this->data['data']->sex);
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCrew', '- Tên thuyền viên', 'required');
        $this->form_validation->set_rules('rank', '- Rank', 'required');
        $this->form_validation->set_rules('sex', '- Giới tính', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'vinalines-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcrew->editCrewDb($this->data['id'],$img);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thuyền viên thành công");window.location.href="'.$this->data['baseurl'].'crew.html";</script>');
            }
        }
        $this->data['action'] = 'editcrew';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletecrew()
    {
        $run = $this->Mcrew->deleteCrewDB($this->session->userdata('username'));
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa thuyền viên thành công");window.location.href="'.$this->data['baseurl'].'crew.html";</script>');
        }
    }
        
    
}
